package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.view.View;

import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.PostVH;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centanet.framework.base.BaseAdapter1;

import java.util.ArrayList;

/**
 * 地图找房租售列表
 * Created by guilin on 16/1/26.
 */
public class PostMapListAdapter extends BaseAdapter1<PostVH> {

    private final DrawableRequestBuilder<String> requestBuilder;
    private final ArrayList<PostDo> list;
    private int viewType = 0;
    private LatLng location;
    private RecyclerViewItemOnClickListener<PostDo> itemOnClickListener;

    public PostMapListAdapter(Context context, DrawableRequestBuilder<String> requestBuilder, ArrayList<PostDo> list) {
        this(context, requestBuilder, list, 0);
    }

    public PostMapListAdapter(Context context, DrawableRequestBuilder<String> requestBuilder,
                              ArrayList<PostDo> list, int viewType) {
        super(context);
        this.requestBuilder = requestBuilder;
        this.list = list;
        this.viewType = viewType;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public void setLocation(LatLng location) {
        this.location = location;
    }

    public void setItemOnClickListener(RecyclerViewItemOnClickListener<PostDo> itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_post_list;
    }

    @Override
    protected PostVH viewHolder(View view) {
        return new PostVH(view);
    }

    @Override
    public void onBindViewHolder(final PostVH holder, int position) {
        if ("s".equalsIgnoreCase(list.get(position).getPostType())) {
            holder.loadSale(requestBuilder, viewType, list.get(position), location);
        } else {
            holder.loadRent(requestBuilder, viewType, list.get(position), location);
        }
        if (itemOnClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemOnClickListener.onItemOnClick(list.get(holder.getAdapterPosition()), 0);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
